home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_4.9 / IMGGRID / IMGGRID.C next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.8 KB  |  153 lines

  1. /* 
  2.  * imggrid.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* IMGGRID:     program superimposes a grid with chosen spacing over an image
  10.  *                    usage: imggrid imimg outimg [-s SPACING] [-i] [-L]
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <images.h>
  18. #include <tiffimage.h>          /* picfile info on images */
  19. extern void print_sos_lic ();
  20.  
  21. #define SPACING 20              /* default spacing of grid */
  22.  
  23. int usage (short);
  24. int input (int, char **, long *, short *);
  25.  
  26. main (argc, argv)
  27.      int argc;
  28.      char *argv[];
  29. {
  30.   register int x, y;            /* image coordinates */
  31.   Image *imgI;                  /* input, output image pointers */
  32.   unsigned char **imgIn;        /* first and second input image */
  33.   long spacing;                 /* spacing of grid lines */
  34.   short invertFlag;             /* invert grid 1/0 for high/low intensity */
  35.   long xSize, ySize;            /* sidelengths of image */
  36.  
  37. /* read user parameter values */
  38.   if ((input (argc, argv, &spacing, &invertFlag)) < 0)
  39.     return (-1);
  40.  
  41. /* read input image */
  42.   imgI = ImageIn (argv[1]);
  43.   if (imgI->bps == 8 && imgI->spp == 3) {
  44.     printf ("Got RGB image!!!\nInput image must be Grayscale or B&W!!\n");
  45.     exit (1);
  46.   }
  47.   imgIn = imgI->img;
  48.   ySize = ImageGetHeight (imgI);
  49.   xSize = ImageGetWidth (imgI);
  50.  
  51. /* create image */
  52.   for (y = 0; y < ySize; y++) {
  53.     if ((y % spacing) == 0) {
  54.       for (x = 0; x < xSize; x++) {
  55.         if (invertFlag) {
  56.           if (imgIn[y][x] > 128)
  57.             imgIn[y][x] = 0;
  58.           else
  59.             imgIn[y][x] = 255;
  60.         }
  61.         else
  62.           imgIn[y][x] = 255;
  63.       }
  64.     }
  65.     for (x = 0; x < xSize; x++) {
  66.       if ((x % spacing) == 0) {
  67.         if (invertFlag) {
  68.           if (imgIn[y][x] > 128)
  69.             imgIn[y][x] = 0;
  70.           else
  71.             imgIn[y][x] = 255;
  72.         }
  73.         else
  74.           imgIn[y][x] = 255;
  75.       }
  76.     }
  77.   }
  78.  
  79. /* write out image */
  80.   ImageOut (argv[2], imgI);
  81.   return (0);
  82. }
  83.  
  84.  
  85. /* USAGE:       function gives instructions on usage of program
  86.  *                    usage: usage (flag)
  87.  *              When flag is 1, the long message is given, 0 gives short.
  88.  */
  89.  
  90. int
  91. usage (flag)
  92.      short flag;                /* flag =1 for long message; =0 for short message */
  93. {
  94.  
  95. /* print short usage message or long */
  96.   printf ("USAGE: imggrid imimg outimg [-s SPACING] [-i] [-L]\n");
  97.   if (flag == 0)
  98.     return (-1);
  99.  
  100.   printf ("\nimggrid creates test image of grid\n");
  101.   printf ("with chosen spacing.\n\n");
  102.   printf ("ARGUMENTS:\n");
  103.   printf ("    inimg: input image filename (TIF)\n");
  104.   printf ("   outimg: output image filename (TIF)\n\n");
  105.   printf ("OPTIONS:\n");
  106.   printf ("  -s SPACING: spacing of grid lines. Default=%d pixels.\n", SPACING);
  107.   printf ("          -i: inverts grid high/low for low/high image.\n");
  108.   printf ("          -L: print Software License for this module\n");
  109.  
  110.   return (-1);
  111. }
  112.  
  113.  
  114. /* INPUT:       function reads input parameters
  115.  *                  usage: input (argc, argv, &spacing, &invertFlag)
  116.  */
  117.  
  118. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  119.  
  120. int
  121. input (argc, argv, spacing, invertFlag)
  122.      int argc;
  123.      char *argv[];
  124.      long *spacing;             /* spacing of grid lines */
  125.      short *invertFlag;         /* inverts grid 1/0 for image intensity */
  126. {
  127.   long n;
  128.  
  129.   if (argc == 1 || argc == 2)
  130.     USAGE_EXIT (1);
  131.  
  132.   *spacing = SPACING;
  133.   *invertFlag = 0;
  134.  
  135.   for (n = 3; n < argc; n++) {
  136.     if (strcmp (argv[n], "-s") == 0) {
  137.       if (++n == argc)
  138.         USAGE_EXIT (0);
  139.       *spacing = atol (argv[n]);
  140.     }
  141.     else if (strcmp (argv[n], "-i") == 0)
  142.       *invertFlag = 1;
  143.     else if (strcmp (argv[n], "-L") == 0) {
  144.       print_sos_lic ();
  145.       exit (0);
  146.     }
  147.     else
  148.       USAGE_EXIT (0);
  149.   }
  150.  
  151.   return (0);
  152. }
  153.